package com.market.sys.api.client;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.market.sys.common.Result;
import com.market.sys.entity.UserLike;
import com.market.sys.service.IUserLikeService;
import com.market.sys.util.TimeUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/userLike")
public class UserLikeController {

    @Resource
    private IUserLikeService userLikeService;

    @PostMapping("/addLike")
    public Result<?> addLike(@RequestBody Map<String,Object> paramsCondition){
        UserLike userLike = BeanUtil.fillBeanWithMap(paramsCondition, new UserLike(), false);
        userLike.setLikeTime(TimeUtil.timeYmdhms(new Date()));
        if (userLikeService.save(userLike)){
            return Result.success("点赞成功");
        }else {
            return Result.error("点赞失败");
        }
    }

    @PostMapping("/cancelLike")
    public Result<?> cancelLike(@RequestBody Map<String,Object> paramsCondition){
        UserLike userLike = BeanUtil.fillBeanWithMap(paramsCondition, new UserLike(), false);
        if (userLikeService.remove(new LambdaQueryWrapper<UserLike>().eq(UserLike::getUserId,userLike.getUserId())
                .eq(UserLike::getThingId,userLike.getThingId()).eq(UserLike::getType,userLike.getType()))){
            return Result.success("取消点赞成功");
        }else {
            return Result.error("取消点赞失败");
        }
    }

    @PostMapping("/isLike")
    public Result<?> isLike(@RequestBody Map<String,Object> paramsCondition){
        UserLike userLike = BeanUtil.fillBeanWithMap(paramsCondition, new UserLike(), false);
        if (userLikeService.query().eq("thing_id",userLike.getThingId()).eq("user_id",userLike.getUserId())
                .eq("type",userLike.getType()).one() != null){
            return Result.success();
        }else {
            return Result.error();
        }
    }
}
